package com.maserb.monitor.service.author_manage.impl;

import com.maserb.monitor.entity.Function;
import com.maserb.monitor.entity.Role;
import com.maserb.monitor.entity.RoleFunction;
import com.maserb.monitor.repository.FunctionRepository;
import com.maserb.monitor.repository.RoleFunctionRepository;
import com.maserb.monitor.repository.RoleRepository;
import com.maserb.monitor.service.author_manage.RoleService;
import com.maserb.monitor.service.base.IToolService;
import com.maserb.monitor.service.base.impl.JpaSpecificationServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl extends JpaSpecificationServiceImpl<RoleRepository, RoleRepository, Role, String>
        implements RoleService {

    @Autowired
    private IToolService toolService;
    @Autowired
    private RoleFunctionRepository roleFunctionRepository;
    @Autowired
    private FunctionRepository functionRepository;
    @Override
    public Page<Role> findAll(Pageable pageable, final String roleName) {
        return this.findAll(getSpecification(roleName),pageable);
    }

    private Specification<Role> getSpecification(final String roleName){
        return new Specification<Role>() {
            @Override
            public Predicate toPredicate(Root<Role> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<String> pathRoleName = root.get("roleName");

                ArrayList<Predicate> list = new ArrayList<Predicate>();

                if (!StringUtils.isEmptyOrWhitespace(roleName)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.like(pathRoleName, "%" + roleName + "%")));
                }

                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        };
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void saveRole(Role role, String[] functionId) {
        save(role);

        //先删除已有的角色权限
        List<RoleFunction> dbFunctionList = roleFunctionRepository.findByRole(role);
        if(dbFunctionList != null && dbFunctionList.size() > 0) {
            roleFunctionRepository.deleteAll(dbFunctionList);
        }
        List<RoleFunction> functionList = new ArrayList<RoleFunction>();
        for(String funId : functionId) {
            if(!StringUtils.isEmptyOrWhitespace(funId)) {
                Function fun = functionRepository.findById(funId).get();
                RoleFunction roleFun = new RoleFunction();
                roleFun.setRole(role);
                roleFun.setTSFunction(fun);
                functionList.add(roleFun);
            }
        }

        roleFunctionRepository.saveAll(functionList);
    }

    @Override
    public List<RoleFunction> findFunctionByRole(Role role) {
        return roleFunctionRepository.findByRole(role);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRoleByKeys(List<String> keys) {
        for(String key : keys) {
            Role role = repository.findById(key).get();

            List<RoleFunction> roleFunctionList = roleFunctionRepository.findByRole(role);

            roleFunctionRepository.deleteAll(roleFunctionList);
            repository.delete(role);
        }
    }
}